<template>
<view class="container" scroll-y="true" bindscrolltolower="onListReachBottom">
    <view class="topbar">
        <view :class="(status == '0' ? 'active' : '')" @tap="changeBar" data-status="0">全部</view>
        <view :class="(status == '-1' ? 'active' : '')" @tap="changeBar" data-status="-1">待付款</view>
        <view :class="(status == '1' ? 'active' : '')" @tap="changeBar" data-status="1">未消费</view>
        <view :class="(status == '2' ? 'active' : '')" @tap="changeBar" data-status="2">待评价</view>
    </view>
    <scroll-view scroll-y="true" :scroll-top="scrollTop" :style="'height:' + order_list_height + 'px'" bindscrolltolower="onListReachBottom">
        <block v-for="(item, order_list) in order_list" :key="order_list">
            <view class="item" :data-order_id="item.order_id" @tap="goDetail">
                <view class="header th100">订单编号：{{item.real_orderid}}</view>
                <view class="content cf">
                    <view class="fl pos">
                        <image :src="item.list_pic"></image>
                        <block v-if="item.pin_num > 0">
                            <div class="fight"></div>
                        </block>
                    </view>
                    <view class="box">
                        <view class="name">{{item.s_name}}</view>
                        <view class="info">数量：{{item.num}} 总价：￥{{item.total_money}}</view>
                    </view>
                </view>
                <view class="footer cf th80">
                    <view class="fl">
                        <text>{{item.status_txt}}</text>
                    </view>
                    <view class="fl" v-if="item.pin_status_txt != ''">
                        <text class="red">({{item.pin_status_txt}})</text>
                    </view>

                    <view class="fr" v-if="item.refund_status" :data-index="index" @click.stop="go" :data-order_id="item.order_id">取消订单</view>
                    <view class="fr" v-else-if="item.paid == 0" @click.stop="goPay" :data-index="index" :data-order_id="item.order_id">去付款</view>
                    <view class="fr" v-else-if="item.status == 1" @click.stop="goReply" :data-index="index" :data-order_id="item.order_id">去评价</view>
                </view>
            </view>
        </block>
    </scroll-view>
</view>
</template>

"<script>
// pages/my/group_order.js
import common from '../../utils/common.js'
var app = getApp();
var sysRes = {};
var rpxUnit = 0;
var page = 1;
var total = 0;
var go_order_id = 0;
var go_order_index = 0;

export default {
  data() {
    return {
      mer_id: 0,
      status: 0,
      order_list: [],
      order_list_height: 0,
      scrollTop: 0,
      order_id: 0
    };
  },

  onUnload: function () {// 页面关闭
  },
  onHide: function () {// 页面隐藏
  },
  onShow: function () {// 页面显示
  // #ifdef H5
  common.hideWxShare()
  // #endif
  },
  onReady: function () {
    // 页面渲染完成
    sysRes = wx.getSystemInfoSync();
    rpxUnit = sysRes.windowWidth / 750;
    this.setData({
      order_list_height: sysRes.windowHeight - 84 * rpxUnit
    });
  },
  onLoad: function (options) {
    if (!options.mer_id && app.merchantOwn) {
      options.mer_id = app.merchantOwn.mer_id;
    }

    if (options.mer_id) {
      this.setData({
        mer_id: options.mer_id
      });
    }

    if (!app.globalData.userInfo) {
      var that = this; //调用应用实例的方法获取全局数据

      common.getUserInfo(function (userInfo) {
        that.setMainContent();
      }, 'setting');
    } else {
      this.setMainContent();
    }
  },
  components: {},
  props: {},
  methods: {
    goPay: function (e) {
      go_order_id = e.currentTarget.dataset.order_id;
      go_order_index = e.currentTarget.dataset.index;
      wx.navigateTo({
        url: "../pay/index?order_id=" + e.currentTarget.dataset.order_id + '&type=group'
      });
    },
    goReply: function (e) {
      go_order_id = e.currentTarget.dataset.order_id;
      go_order_index = e.currentTarget.dataset.index;
      wx.navigateTo({
        url: "my_reply?order_id=" + e.currentTarget.dataset.order_id
      });
    },
    goDetail: function (e) {
      console.log(e.currentTarget.dataset);
      go_order_id = e.currentTarget.dataset.order_id;
      go_order_index = e.currentTarget.dataset.index;
      wx.navigateTo({
        url: "group_order_detail?order_id=" + e.currentTarget.dataset.order_id
      });
    },
    setOneContent: function (result) {
      if (this.order_id == 0) {
        this.order_list = [];
      }

      this.setData({
        order_list: this.order_list.concat(result.list),
        order_id: result.order_id
      });
      console.log(this.order_id);
      wx.hideToast();
    },
    getOrderList: function () {
      common.post('My&a=group_order_list', {
        order_id: this.order_id,
        status: this.status,
        mer_id: this.mer_id
      }, "setOneContent", this);
    },
    changeBar: function (e) {
      if (this.status == e.currentTarget.dataset.status) {
        return false;
      }

      this.setData({
        status: e.currentTarget.dataset.status,
        scrollTop: 0,
        order_id: 0
      });
      this.getOrderList();
    },
    onListReachBottom: function (e) {
      if (this.order_id == 0) {
        return false;
      }

      this.getOrderList();
    },
    setMainContent: function () {
      common.post('My&a=group_order_list', {
        mer_id: this.mer_id
      }, "setOneContent", this, 'back');
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "shop_order.css";
.pos{ position: relative}
.fight{width: 80rpx; height: 80rpx;  position: absolute; z-index: 9; top: 0; left: 0; background: url(https://hf.pigcms.com/static/wxapp/ping.png) no-repeat; background-size: 80rpx 80rpx;}
</style>